package com.fivetalks.client;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import org.apache.http.HttpEntity;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.TabHost;

import com.fivetalks.network.Definition;
import com.fivetalks.object.Member;

public class MainFrameActivity extends TabActivity {
	private final String URL = Definition.URL_ROOT + "/member/getinfo";
	String id;
	String email;
	String password;
	String nickname;
	String age;
	String gender;
	Member member;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Intent intent = getIntent();
		email = intent.getStringExtra("email");
		password = intent.getStringExtra("password");
		Log.d("MAINFRAME","하하하하하하하메인프레임에 오신걸 환영합니다.");
		GetAsyncTask gat = new GetAsyncTask();
		gat.execute(URL);

	}

	class GetAsyncTask extends AsyncTask<String, Integer, String> {
		Vector<BasicNameValuePair> nameValue = new Vector<BasicNameValuePair>();

		// String...은 개수가 여러개 가변적으로 받아들이고 params에 배열로 저장
		// 백그라운드로 동작할 메소드
		// 보통시간이 오래걸리는 작업.
		protected String doInBackground(String... params) {
			// Log.i("getApp","doInBackground");
			nameValue.add(new BasicNameValuePair("email", email));
			nameValue.add(new BasicNameValuePair("password", password));
			String result = null;
			try {
				result = sendData(params[0]);
				// Log.i("getApp",result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return result;
		}

		// doInBackground메소드가 수행된 결과를 받아들여서 수행된다.
		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);

			// Log.i("getApp","onPostExecute");

			List<String> start_tag_list = new ArrayList<String>();
			start_tag_list.add("<id>");
			start_tag_list.add("<email>");
			start_tag_list.add("<password>");
			start_tag_list.add("<nickname>");
			start_tag_list.add("<age>");
			start_tag_list.add("<gender>");

			List<String> end_tag_list = new ArrayList<String>();
			end_tag_list.add("</id>");
			end_tag_list.add("</email>");
			end_tag_list.add("</password>");
			end_tag_list.add("</nickname>");
			end_tag_list.add("</age>");
			end_tag_list.add("</gender>");

			List<String> val_list = new ArrayList<String>();
			String start_tag, end_tag;
			int i, index_start, index_end;
			for (i = 0; i < start_tag_list.size(); i++) {
				start_tag = start_tag_list.get(i);
				end_tag = end_tag_list.get(i);
				index_start = result.indexOf(start_tag, 0);
				index_end = result.indexOf(end_tag, 0);
				// Log.i("getApp",start_tag+" : "+ index_start);
				// Log.i("getApp",end_tag+ " : "+ index_start);
				val_list.add(result.substring(index_start + start_tag.length(),
						index_end));
			}
			for (i = 0; i < val_list.size(); i++) {
				// Log.i("getApp",val_list.get(i));
			}

			id = val_list.get(0);
			email = val_list.get(1);
			password = val_list.get(2);
			nickname = val_list.get(3);
			age = val_list.get(4);
			gender = val_list.get(5);

			member = new Member(Long.parseLong(id), email, password, nickname,
					Integer.parseInt(age), gender);

			// Log.i("getApp",""+member.getId());
			// Log.i("getApp",""+member.getEmail());
			// Log.i("getApp",""+member.getPassword());
			// Log.i("getApp",""+member.getNickname());
			// Log.i("getApp",""+member.getAge());
			// Log.i("getApp",""+member.getGender());

			nameValue.clear();

			final TabHost tabHost = getTabHost();

			Intent intent1 = new Intent(MainFrameActivity.this,
					ChatLobbyActivity.class);
			Intent intent2 = new Intent(MainFrameActivity.this,
					SingleChatActivity.class);
			Intent intent3 = new Intent(MainFrameActivity.this,
					SettingActivity.class);
			intent1.putExtra("member", member);
			intent2.putExtra("member", member);
			intent3.putExtra("member", member);

			// 클릭할때 마다 리플레쉬
			Drawable chatroom = getResources().getDrawable(R.drawable.chatroom);
			Drawable loveroom = getResources().getDrawable(R.drawable.loveroom);
			Drawable setting = getResources().getDrawable(R.drawable.setting);
			tabHost.addTab(tabHost
					.newTabSpec("tab1")
					.setIndicator("",chatroom)
					.setContent(
							intent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
			tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.BLACK);
			tabHost.getTabWidget().setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
			// 클릭할때 마다 리플레쉬
			tabHost.addTab(tabHost
					.newTabSpec("tab2")
					.setIndicator("",loveroom)
					.setContent(
							intent2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
			tabHost.getTabWidget().getChildAt(1).setBackgroundColor(Color.BLACK);
			// 클릭할때 마다 리플레쉬
			tabHost.addTab(tabHost
					.newTabSpec("tab3")
					.setIndicator("",setting)
					.setContent(
							intent3.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
			tabHost.getTabWidget().getChildAt(2).setBackgroundColor(Color.BLACK);
		}

		public String sendData(String url) throws Exception {

			HttpPost request = makeHttpPost(url);

			HttpClient client = new DefaultHttpClient();
			ResponseHandler<String> reshandler = new BasicResponseHandler();
			String result = client.execute(request, reshandler);

			return result;

		}

		private HttpPost makeHttpPost(String url) throws Exception {
			HttpPost request = new HttpPost(url);

			request.setEntity(makeEntity(nameValue));

			return request;
		}

		private HttpEntity makeEntity(Vector<BasicNameValuePair> nameValue)
				throws Exception {
			HttpEntity result = null;
			try {
				result = new UrlEncodedFormEntity(nameValue, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			return result;
		}

	}
}